package com.example.administrator.testxingpiao.ui.main.homepage;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.administrator.testxingpiao.bean.BanerList1;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/5.
 */
public class TopPagerAdapter extends PagerAdapter {

    private List<BanerList1> banerList;
    private List<ImageView> mList;
    private Context context;


    public TopPagerAdapter(List<BanerList1> list,Context context){

        this.banerList = list;
        this.context = context;
        mList = new ArrayList<>();
        initImageList();
    }

    private void initImageList() {
        for (int i = 0; i < banerList.size(); i++) {
            ImageView imageView = new ImageView(context);
            Glide.with(context).load(banerList.get(i).getImage()).into(imageView);
            mList.add(imageView);
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {

        container.addView(mList.get(position));
        mList.get(position).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,CampaignWebActivity.class);
                intent.putExtra("url", banerList.get(position).getUrlWap());
                context.startActivity(intent);
            }
        });

        return mList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView(mList.get(position));
    }
}
